package com.springboot.template.thread.pool;


import java.util.concurrent.ArrayBlockingQueue;

public class WorkQueueTest {
    public static void main(String[] args) throws InterruptedException {
        // 创建一个容量为3的有界阻塞队列
        ArrayBlockingQueue<Integer> workQueue = new ArrayBlockingQueue(3);

        workQueue.put(1);
        workQueue.put(2);
        workQueue.put(3);

        // 试图添加第四个元素时会阻塞，因为队列已满
        new Thread(()->{
            try {
                workQueue.put(4);
                System.out.println("Added 4");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
        // 让主线程休眠一会儿，以确保子线程被阻塞
        Thread.sleep(2000);

        System.out.println("Removed: " + workQueue.take());
        // 现在队列有空间，子线程可以继续执行
        Thread.sleep(2000);
        System.out.println("Removed: " + workQueue.take());
        System.out.println("Removed: " + workQueue.take());
    }
}
